import rightBg from '@/assets/images/center-bg.png'
import { requestGet, requestPost } from '@/api/system/common'

export function notice(options = {}) {
  return {
    data() {
      return {
        rightBg: rightBg,
        form: {
          checkList: []
        },
        pager: 0,
        query: {
          blurry: '',
          page: 1,
          size: 10
        },
        checkList: [],
        noReadList: [], // 未读消息模块
        readList: [], // 已读消息模版
        disabled: true, // 按钮显示隐藏
        checkedAll: false, // 是否全选状态
        isIndeterminate: false, // 半选状态
        disabledButton: true, // 已读消息按钮
        checkedButtonAll: false, // 是否全选状态
        isButtonIndeterminate: false,
        borderStyle: {}
      }
    },
    watch: {
      themes: {
        handler(val, oldVal) {
          if (val) {
            this.borderStyle.borderRadius = val.bkdx
            // if (this.temp.sfxsbk === true) {
            this.borderStyle.border = '1px solid ' + val.bkys
            // }
            // if (this.temp.bkyy === true) {
            this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)'
            // }
            this.borderStyle.background = val.columnGroundColor
            this.borderStyle.color = val.columnTitleFontColor
            this.borderStyle = Object.assign({}, this.borderStyle)
          }
        },
        immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
        deep: false // formType为对象时，设置为true，此属性监听对象发生变化
      }
    },
    computed: {
      rightBack() {
        return { background: 'url(' + this.rightBg + ') no-repeat 100% 100%', backgroundSize: '100% 100%' }
      },
      themes() {
        return this.$store.getters.theme
      }
    },
    created() {
      this.initAllUserNoReadNotice()
      this.query.page = 1
      this.query.size = 10
      this.query.blurry = ''
      this.checkList = []
      this.form.checkList = []
      this.initAllUserReadNotice()
    },
    methods: {
      // 查询未读
      initAllUserNoReadNotice() {
        requestGet(options.url + '/message', {})
          .then(res => {
            const { data } = res
            this.noReadList = data.data
          }).catch(() => {
            this.noReadList = []
          })
      },
      // 查询已读消息
      initAllUserReadNotice() {
        this.checkList = []
        requestGet(options.url + '/pager', this.query)
          .then(res => {
            const { data } = res
            this.readList = data.data
            this.pager = data.pagers
          }).catch(() => {

          })
      },
      // 未读消息选择按钮
      handleCheckedCitiesChange(val) {
        const checkedCount = val.length
        this.checkList = val
        this.checkedAll = checkedCount === this.noReadList.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.noReadList.length
        this.disabled = this.form.checkList.length <= 0
      },
      // 已读消息选择按钮
      handleCheckedButtonChange(val) {
        const checkedCount = val.length
        this.checkList = val
        this.checkedButtonAll = checkedCount === this.readList.length
        this.isButtonIndeterminate = checkedCount > 0 && checkedCount < this.readList.length
        this.disabledButton = this.checkList.length <= 0
      },
      // 未读消息全选，全部选
      checkedObject() {
        this.isIndeterminate = false
        if (this.checkedAll) {
          if (this.noReadList.length > 0) {
            this.noReadList.forEach(item => {
              this.form.checkList.push(item.id)
            })
          } else {
            this.form.checkList = []
          }
        } else {
          this.form.checkList = []
        }
        this.disabled = this.form.checkList.length <= 0
      },
      // 已读消息全选，全不选
      checkedButtonObject() {
        this.isButtonIndeterminate = false
        if (this.checkedButtonAll) {
          if (this.readList.length > 0) {
            this.readList.forEach(item => {
              this.checkList.push(item.id)
            })
          } else {
            this.checkList = []
          }
        } else {
          this.checkList = []
        }
        this.disabledButton = this.checkList.length <= 0
      },
      // 删除消息
      deleteAll(data) {
        requestPost(options.url + '/del', data)
          .then(res => {
            this.initAllUserNoReadNotice()
            this.$message.success('删除成功！')
            this.initAllUserReadNotice()
          }).catch(() => {

          })
      },
      // 标记已读
      updateAll(data) {
        requestPost(options.url + '/save', data)
          .then(res => {
            this.initAllUserNoReadNotice()
            this.$message.success('标记成功！')
            this.query.page = 1
            this.initAllUserReadNotice()
          }).catch(() => {

          })
      },
      // 搜索框事件
      queryObject() {
        this.query.page = 1
        this.initAllUserReadNotice()
      },
      // 查看详情
      openEntity(id, url) {
        const data = []
        data.push(id)
        this.updateAll(data)
        window.open(url, '_blank')
      }
    }
  }
}
